package com.tao.content.api;

import com.alibaba.fastjson.JSON;
import com.tao.base.model.Result;
import com.tao.content.domain.dto.CoursePreviewDto;
import com.tao.content.domain.pojo.CoursePublish;
import com.tao.content.domain.pojo.Teachplan;
import com.tao.content.domain.vo.CourseBaseInfoVo;
import com.tao.content.service.impl.CoursePublishServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * Freemarker测试
 * </p>
 *
 * @author taohongrun
 * @since 2024/11/16
 */
@RestController
@Tag(name = "课程预览发布接口",description = "课程预览发布接口")
public class CoursePublishController {
    @Autowired
    CoursePublishServiceImpl coursePublishService;

    @GetMapping("/coursepreview/{courseId}")
    public ModelAndView preview(@PathVariable("courseId") Long courseId) {
        //获取课程预览信息
        CoursePreviewDto coursePreviewInfo = coursePublishService.getCoursePreviewInfo(courseId);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("model", coursePreviewInfo);
        modelAndView.setViewName("course_template");
        return modelAndView;
    }


    @Operation(description = "课程发布")
    @PostMapping("/coursepublish/{courseId}")
    public Result coursepublish(@PathVariable("courseId") Long courseId) {
        Long companyId = 1232141425L;
        coursePublishService.publish(companyId,courseId);
        return Result.success();
    }

    @Operation(summary = "查询课程发布基本信息")
    @GetMapping("/r/coursepublish/{courseId}")
    public Result<CoursePublish> getCoursepublish(@PathVariable("courseId") Long courseId) {
        CoursePublish coursePublish = coursePublishService.getCoursePublish(courseId);
        return Result.success(coursePublish);
    }

    @Operation(summary = "获取课程发布详细信息")
    @GetMapping("/course/whole/{courseId}")
    public Result<CoursePreviewDto> getCoursepublishPreview(@PathVariable("courseId") Long courseId) {
        CoursePreviewDto coursePreviewDto = new CoursePreviewDto();
        CoursePublish coursePublish = coursePublishService.getCoursePublish(courseId);
        if(coursePublish == null) return  Result.success(coursePreviewDto);

        CourseBaseInfoVo courseBaseInfoVo =new CourseBaseInfoVo();
        List<Teachplan> teachplans = new ArrayList<>();
        BeanUtils.copyProperties(coursePublish,courseBaseInfoVo);
        teachplans = JSON.parseArray(coursePublish.getTeachplan(),Teachplan.class);
        coursePreviewDto.setCourseBase(courseBaseInfoVo);
        coursePreviewDto.setTeachplans(teachplans);
        return Result.success(coursePreviewDto);
    }
}
